import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		hasBind: false,
		hasStart: uni.getStorageSync('HAS_START') == '1',
		hasJoin: uni.getStorageSync('HAS_JOIN') == '1',
		userId: '',
		openId: '',
		userName: '',
		headImg: '',
		deptId: '',
		deptName: '',
		tabbarIndex:0,
		tabbarName:'首页',
	},
	mutations: {
		SET_USERINFO(state, userInfo){
			state.hasLogin = true;
			state.openId = userInfo.openId
			if(userInfo.userName){
				state.hasBind = true
				state.userId = userInfo.id;
				state.userName = userInfo.userName;
				state.headImg = userInfo.headImg;
				state.deptId = userInfo.deptId;
				state.deptName = userInfo.deptName;
			}
		},
		SET_JOIN(state){
			uni.setStorageSync('HAS_JOIN', '1')
			state.hasJoin = true
		},
		SET_START(state){
			uni.setStorageSync('HAS_START', '1')
			state.hasStart = true
		},
		changeTabbarIndex(state,val){
			state.tabbarIndex = val
		},
		changeTabbarName(state,val){
			state.tabbarName = val
		}
		
	},
	getters: {
		userId: (state, getters) => {
    		return state.userId
  		},
		deptId: (state, getters) => {
    		return state.deptId
  		},
	},
	actions: {
		Login({ commit }, userInfo) {
			console.log('userInfo====', userInfo)
			commit('SET_USERINFO', userInfo)
		},
		Join({ commit }) {
			commit('SET_JOIN')
		},
		Start({ commit }) {
			commit('SET_START')
		},
	}
})

export default store
